//引入express
const { request } = require('express');
const express = require('express')
//mysql驱动
const mysql = require('mysql');
//连接数据库
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'h52107'
});
connection.connect();
//创建实例
const app = express()
//托管静态资源
app.use(express.static('public'))
// 用于处理请求主体中的数据，使用POST请求的时候需要加上这两段代码
app.use(express.json()) // for parsing application/json  当提交的数据为json格式时
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded 当提交的数据是普通文本格式时
//处理登录数据
app.get('/login.action', function (request, response, next) {
    //从请求中获取用户名和密码
    const {
        username,
        password
    } = request.query
    /* console.log(username);
    console.log(password); */
    //创建sql语句
    const sql = 'select count(*) as total from users where username=? and password=?'
    //到数据库中验证用户名和密码
    const params = [username, password]
    connection.query(sql, params, function (err, result) {
        if (err) {
            response.send('有异常')
            return
        }
        //console.log(result);
        if (result[0].total === 1) {
            response.send('登录成功')
        } else {
            response.send('登录失败')
        }
    })
    //connection.end()
    //response.send('成功')
})

app.post('/login.action',(request,response,next)=>{
    const {username,password}=request.body
    const sql = 'select count(*) as total from users where username=? and password=?'
    const params=[username,password]
    connection.query(sql,params,(err,result)=>{
        if(err){
            response.json({
                code:800,
                data:null,
                message:'数据库处理异常'
            })
            return
        }
        console.log('result:'+result);
        if(result[0].total===1){
            response.json({
                code:200,
                data:{
                    username
                }
            })
        }else{
            response.json({
                code:400,
                data:null,
                message:'用户名或密码错误'
            })
        }
    })
})

//处理GET请求，注册功能
app.get('/regist.action', function (request, response, next) {
    const {
        registname,
        registpwd
    } = request.query
    const sql = 'insert into users(username,password) values(?,?)'
    const addParams = [registname, registpwd]
    connection.query(sql, addParams, function (err, result) {
        if (err) {
            console.log('有异常');
            return
        }
        response.send('注册成功')
    })
})
//处理POST请求，注册功能
app.post('/regist.action',function(request,response,next){
    const {
        registname,
        registpwd
    } = request.body
    const sql = 'insert into users(username,password) values(?,?)'
    const addParams = [registname, registpwd]
    connection.query(sql, addParams, function (err, result) {
        if (err) {
            console.log('有异常');
            return
        }
        response.send('注册成功')
    })
})
//使用get请求查询用户名是否存在
app.get('/exist',function(request,response,next){
    const {username}=request.query
    const sql='select count(*) as count from users where username=?'
    const params=[username]
    connection.query(sql,params,function(err,res){
        if(err){
            response.send('有错误')
            return
        }
        if(res[0].count>=1){
            response.send('已存在该用户')
        }else{
            response.send('用户名可使用')
        }
    })
})
//使用post请求查询用户名是否存在
app.post('/exist',function(requset,response,next){
    const {username}=requset.body
    const sql='select count(*) as count from users where username=?'
    const params=[username]
    connection.query(sql,params,function(err,res){
        if(err){
            response.send('有错误')
            return
        }
        if(res[0].count>=1){
            response.send('已存在该用户')
        }else{
            response.send('用户名可使用')
        }
    })
})

app.listen(8000, function () {
    console.log('Server running at http://localhost:8000');
})
